#include "factory.h"



void cmd_ls(puserNode puser,factory_t * pfac)
{
    //接收参数
    printf("cmd_ls runing...\n");
    printf("name :%s  path: %s\n", puser->userName, puser->homePath);
    char pwdbuf[PWD_LENGTH] = {0};
    size_t pwdLen = 0;
    recvCyc(puser->fd, &pwdLen, sizeof(int));
    recvCyc(puser->fd, pwdbuf,pwdLen );
    printf("recv pwd: %s\n",pwdbuf);
    //处理成 服务器本地地址
    char localPwd[PWD_LENGTH] = {0};
    sprintf(localPwd, "%s%s", puser->homePath, pwdbuf); //宏的字符串能不能当参数？
    printf("local pwd: %s\n",localPwd);

    //处理
    train_t sender = {0};
    char dirPath[PATH_LENGTH] = {0};

    struct stat statbuf;
    struct dirent *pdirent;
    DIR *pdir = opendir(localPwd);
    if(pdir == NULL)
    {
        perror("opendir in smd_ls");
        return;
    }

    while(pdirent = readdir(pdir))
    {
        if(strcmp(pdirent->d_name,".") == 0)
            continue;
        sprintf(dirPath, "%s%s%s",localPwd, "/", pdirent->d_name);
        stat(dirPath, &statbuf);
        
        
        //加判断：
        // S_ISREG()      普通文件
        // S_ISDIR()      目录文件
        // S_ISCHR()      字符特殊文件
        // S_ISBLK()      块特殊文件
        // S_ISFIFO()     管道
        // S_ISLNK()      符号链接
        // S_ISSOCK()     套接字
        sprintf(sender.buf, "%5d    %20s    %10ldB", pdirent->d_type, pdirent->d_name, statbuf.st_size);
        sender.dataLen = strlen(sender.buf);
        send(puser->fd, &sender, sizeof(int) + sender.dataLen, 0);
    }
    //发送结束标志： datalen = -1
    int tmp = -1;
    send(puser->fd,&tmp,sizeof(int),0);

    closedir(pdir);
    return;
}

void cmd_cd(puserNode puser,factory_t * pfac)
{
    //cd要做的事情：服务器检查参数是否正确，返回给客户端允许cd或不允许cd，客户端接到后自己把pwd改了
    
    printf("cd runing...\n");
    printf("name :%s  path: %s\n", puser->userName, puser->homePath);
    
    
    char tagbuf[PATH_LENGTH] = {0};
    size_t pwdLen = 0;
    recvCyc(puser->fd, &pwdLen, sizeof(int));
    recvCyc(puser->fd, tagbuf,pwdLen );
    printf("recv pwd: %s\n",tagbuf);

    //处理成local path
    char dirbuf[PATH_LENGTH] = {0};
    sprintf(dirbuf, "%s%s", puser->homePath, tagbuf);
    printf("local dir: %s\n", dirbuf);
    int ret;
    DIR *tmp = opendir(dirbuf);
    if(tmp)//打开成功
    {
        ret = 1;
        send(puser->fd, &ret, sizeof(int), 0);
    }
    else//打开失败
    {
        ret = 0;
        send(puser->fd, &ret, sizeof(int), 0);
    }
    closedir(tmp);
    return;
}


void cmd_puts(puserNode puser,factory_t * pfac)
{
    //接收文件名，转换成本地路径
    char fileName[PATH_LENGTH] = {0};
    int len = 0;
    recvCyc(puser->fd, &len, sizeof(int));
    recvCyc(puser->fd, fileName, len);

    char localPath[PATH_LENGTH] = {0};
    sprintf(localPath, "%s%s", puser->homePath, fileName);

    //建立传输链接，
    struct sockaddr_in addrClient;
    socklen_t addrlen=sizeof(addrClient);
    int newFd = accept(pfac->transFd, (struct sockaddr *)&addrClient, &addrlen);
    if(newFd == -1)
    {
        perror("accept in gets");
        return;
    }
    printf("client ip=%s,client port=%d\n",inet_ntoa(addrClient.sin_addr),ntohs(addrClient.sin_port));
    printf("new task fd: %d ,connected\n", newFd);
    //创建任务节点，唤醒工人进行文件传输
    ptaskNode_t pnewTask = (ptaskNode_t)calloc(sizeof(taskNode_t), 1);

    pnewTask->fd = newFd;
    strcpy(pnewTask->fileName, localPath);
    pnewTask->mod = 0;  //puts命令是发送文件发送给服务器mod = 0
    pnewTask->pNext = NULL;

    pthread_mutex_lock(&(pfac->que.queMutex));
    que_set(&pfac->que,pnewTask);
    pthread_mutex_unlock(&(pfac->que.queMutex));

    int ret = pthread_cond_signal(&pfac->cond);
    if(ret == -1)
    {
        perror("cond_signal");
    }

}

//
void cmd_gets(puserNode puser,factory_t * pfac)
{
    printf("gets runing...\n");
    //接收文件名，转换为本地路径
    char fileName[PATH_LENGTH] = {0};
    int len = 0;
    recvCyc(puser->fd, &len, sizeof(int));
    recvCyc(puser->fd, fileName, len);

    char localPath[PATH_LENGTH] = {0};
    sprintf(localPath, "%s%s", puser->homePath, fileName);

    printf("gets :localPath: %s\n", localPath);

    train_t sender;
    //打开文件看是否存在，
    int fd = open(localPath, O_RDONLY);
    if(fd == -1)
    {
        sender.dataLen = 0;
        send(puser->fd, &sender.dataLen, sizeof(int), 0);
        return;
    }
    else
    {
        close(fd); 
        sender.dataLen = 1;
    }
    send(puser->fd, &sender.dataLen, sizeof(int), 0);

    //建立新的传输文件连接
    struct sockaddr_in addrClient;
    socklen_t addrlen=sizeof(addrClient);
    int newFd = accept(pfac->transFd, (struct sockaddr *)&addrClient, &addrlen);
    if(newFd == -1)
    {
        perror("accept in gets");
        return;
    }
    printf("client ip=%s,client port=%d\n",inet_ntoa(addrClient.sin_addr),ntohs(addrClient.sin_port));
    printf("new task fd: %d ,connected\n", newFd);
    //存在，创建任务节点，并唤醒传输工人进行传输
    ptaskNode_t pnewTask = (ptaskNode_t)calloc(sizeof(taskNode_t), 1);

    pnewTask->fd = newFd;
    strcpy(pnewTask->fileName, localPath);
    pnewTask->mod = 1;  //gets命令是发送文件发送给客户端mod = 1
    pnewTask->pNext = NULL;

    pthread_mutex_lock(&(pfac->que.queMutex));
    que_set(&pfac->que,pnewTask);
    pthread_mutex_unlock(&(pfac->que.queMutex));

    int ret = pthread_cond_signal(&pfac->cond);
    if(ret == -1)
    {
        perror("cond_signal");
    }
}


